//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.client.geom;

import java.util.ArrayList;
import java.util.List;

public class Point {
	public static final int INVALID = -1;
	public int x;
	public int y;

	public Point(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public Point(Point cp) {
		this.x = cp.x;
		this.y = cp.y;
	}

	public Point(String coord) {
		x = y = INVALID;

		try {
			final List<Integer> values = new ArrayList<Integer>();
			for (String value : coord.split(" ")) {
				values.add(new Integer(value));
			}

			if (values.size() == 2) {
				x = values.get(0);
				y = values.get(1);
			}
		} catch (Exception e) {
		}
	}

	public String toString() {
		return x + " " + y;
	}

	public Point minusEqual(Point rh) {
		x -= rh.x;
		y -= rh.y;
		return this;
	}

	public Point minus(Point rh) {
		return new Point(this).minusEqual(rh);
	}

	public int norm() {
		return x * x + y * y;
	}

	public boolean equals(Object obj) {
		if (!(obj instanceof Point)) {
			return false;
		}

		final Point rh = (Point) obj;
		return x == rh.x && y == rh.y;
	}

	public int hashCode() {
		return x + (y << 16);
	}

	public boolean isValid() {
		return x != INVALID || y != INVALID;
	}
}
